// pages/classanalysis/classanalysis.js
import http from '../../utils/requestAnalysis'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        launchFlag: true,
        selectMenu: {},
        height: null,
        switch: true,
        classInfo: {},
        totalScoreFlag: true,
        criticalGrowthFlag: true,
        classReferenceNumber: 0,
        classAverageScore: 0,
        classHighestScore: 0,
        classAverageScoreRank: 0,
        classMinimumScore: 0,
        lineOnes: 0,
        lineTwos: 0,
        schoolReferenceNumber: 0,
        schoolAverageScore: 0,
        schoolMaximumScore: 0,
        datalist: {},
        criticalGrowthData: {},
        onlineComparison: {},
        scoringRateData: [],
        aline: null,
        bline: null,
        averageScoreData: [],
        averageScoreX: [],
        averageScoreY:[],
        fractionalRangeData: [],
        goOnlineFlag: true,
        yData: [
            {
                name: '班级',
                data: [10,20,30,40]
            },
            {
                name: '学校',
                data:[1,2,3,4]
            }
        ],
        xData: ['优秀','良好','及格','低分'],
        menuList: [
        ],
    },
    changeMenu(e) {
        //console.log(e.detail, 'sssssssssssssssss');
        this.setData({
            selectMenu: e.detail
        })
        this.getScoreOverview()
        // 判断是单科还是总分
        if (e.detail.subjectName.indexOf('总分') > -1) {
            this.setData({
                totalScoreFlag: true
            })
            if (this.data.selectMenu.navType == 0) {
                this.totalScoreScoringRate()
            } else {
                this.analysisClassOnlineComparator()
                this.getCriticalGrowth()
            }
        } else {
            this.setData({
                totalScoreFlag: false
            })
            if (this.data.selectMenu.navType == 0) {
                this.monofamilyScoringRate()
            }
        }
        // if (this.data.selectMenu.navType == 0) {
            this.fractionalRange()
        // }
        // this.onlineSituation()
        this.getExamOverview()
        this.comparisonOfAverageScores()
    },
    changeoverSwitch() {
        this.setData({
            switch: !this.data.switch
        })
    },
    async getSchoolSubject() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        //console.log(datalist, 'looooooooooooooooooooooooool')
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/subject/list',
                method: 'POST',
                data: {
                    navId: datalist.n5,
                    groupId: that.data.type == 1 ? datalist.groupid : that.data.classInfo.classid,
                    groupType: that.data.type == 1 ? 0 : 1
                },
                header: {
                    "content-type": "application/json"
                }
            }
            let res = await http(options)
            //console.info(res)
            if (res.data.code == 0) {
                this.setData({
                    menuList: res.data.data,
                    selectMenu: res.data.data[0]
                })
                this.getScoreOverview()
                // 判断是单科还是总分
                if (res.data.data[0].subjectName.indexOf('总分') > -1) {
                    this.setData({
                        totalScoreFlag: true
                    })
                    if (this.data.selectMenu.navType == 0) {
                        this.totalScoreScoringRate()
                    } else {
                        this.analysisClassOnlineComparator()
                        this.getCriticalGrowth()
                    }
                } else {
                    this.setData({
                        totalScoreFlag: false
                    })
                    if (this.data.selectMenu.navType == 0) {
                        this.monofamilyScoringRate()
                    }
                }
                // if (this.data.selectMenu.navType == 0) {
                    this.fractionalRange()
                // }
                // this.onlineSituation()
                this.getExamOverview()
                this.comparisonOfAverageScores()
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 成绩总览
    async getScoreOverview() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/class/score/overview',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.subjectName.indexOf('总分') > -1 ? datalist.n5 : this.data.selectMenu.navId,
                    groupId: that.data.classInfo.classid,
                    navType:this.data.selectMenu.navType
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            // //console.info(res)
            if (res.data.code == 0) {
                //console.log(res.data.data, 'xxxxxxxxxxxxxxxxxxxxxxxx');
                let data = res.data.data
                this.setData({
                    classReferenceNumber: data.people,
                    classAverageScore: data.avgScore,
                    classHighestScore: data.maxScore,
                    classAverageScoreRank: data.classAvgRank.rank,
                    classMinimumScore: data.minScore
                })
                if (data.aline == null) {
                    this.setData({
                        aline:'一本线'
                    })
                } else if (data.aline == 0) {
                    this.setData({
                        aline:'清北线'
                    })
                } else if (data.aline == 1) {
                    this.setData({
                        aline:'强基线'
                    })
                } else if (data.aline == 2) {
                    this.setData({
                        aline:'600分线'
                    })
                } else if (data.aline == 3) {
                    this.setData({
                        aline:'985线'
                    })
                } else if (data.aline == 4) {
                    this.setData({
                        aline:'211线'
                    })
                } else if (data.aline == 5) {
                    this.setData({
                        aline:'特控线(一本线)'
                    })
                } else if (data.aline == 6) {
                    this.setData({
                        aline:'一本线'
                    })
                } else if (data.aline == 7) {
                    this.setData({
                        aline:'二本线'
                    })
                } else if (data.aline == 8) {
                    this.setData({
                        aline:'本科线'
                    })
                } else if (data.aline == 9) {
                    this.setData({
                        aline:'专科线'
                    })
                }
                if (data.bline == null) {
                    this.setData({
                        bline:'二本线'
                    })
                } else if (data.bline == 0) {
                    this.setData({
                        bline:'清北线'
                    })
                } else if (data.bline == 1) {
                    this.setData({
                        bline:'强基线'
                    })
                } else if (data.bline == 2) {
                    this.setData({
                        bline:'600分线'
                    })
                } else if (data.bline == 3) {
                    this.setData({
                        bline:'985线'
                    })
                } else if (data.bline == 4) {
                    this.setData({
                        bline:'211线'
                    })
                } else if (data.bline == 5) {
                    this.setData({
                        bline:'特控线(一本线)'
                    })
                } else if (data.bline == 6) {
                    this.setData({
                        bline:'一本线'
                    })
                } else if (data.bline == 7) {
                    this.setData({
                        bline:'二本线'
                    })
                } else if (data.bline == 8) {
                    this.setData({
                        bline:'本科线'
                    })
                } else if (data.bline == 9) {
                    this.setData({
                        bline:'专科线'
                    })
                }
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 上线情况
    async onlineSituation() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/class/line',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.subjectName.indexOf('总分') > -1 ? datalist.n5 : this.data.selectMenu.navId,
                    groupId: that.data.classInfo.classid,
                    lineType: 0
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            // //console.info(res)
            if (res.data.code == 0) {
                //console.log(res.data.data, 'xxxxxxxxxxxxxxxxxxxxxxxx');
                let data = res.data.data
                this.setData({
                    lineOnes: data.lineOnes[0].allScoreLine,
                    lineTwos: data.lineTwos[0].allScoreLine,
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 校级成绩总览
    async getExamOverview() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/school/score/overview',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.subjectName.indexOf('总分') > -1 ? datalist.n5 : this.data.selectMenu.navId,
                    groupId: datalist.groupid,
                    navType:this.data.selectMenu.navType
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                this.setData({
                    schoolReferenceNumber:data.people,
                    schoolAverageScore:data.avgScore,
                    schoolMaximumScore:data.maxScore,
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 平均分对比
    async comparisonOfAverageScores() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/class/avg',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.subjectName.indexOf('总分') > -1 ? datalist.n5 : this.data.selectMenu.navId,
                    groupId: that.data.classInfo.classid,
                    navType:this.data.selectMenu.navType
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                let x = []
                let y = []
                data.classAvgDetails.map(res => {
                    x.push(res.className)
                    y.push(res.avgScore)
                })
                this.setData({
                    averageScoreData: data.classAvgDetails,
                    averageScoreX: x,
                    averageScoreY: y
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 临界生
    async getCriticalGrowth() {
        if (!this.data.launchFlag) {
            return
        }
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/class/critical',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.subjectName.indexOf('总分') > -1 ? datalist.n5 : this.data.selectMenu.navId,
                    groupId: that.data.classInfo.classid,
                    groupType:1,
                    lineType: 0
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                //console.log(data,'ggggggggggggggggggggggggggg');
                if (data) {
                    this.setData({
                        criticalGrowthFlag: true
                    })
                } else {
                    this.setData({
                        criticalGrowthFlag: false
                    })
                }
                this.setData({
                    criticalGrowthData: data
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 分数段
    async fractionalRange() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/class/score/paragraph',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.subjectName.indexOf('总分') > -1 ? datalist.n5 : this.data.selectMenu.navId,
                    groupId: that.data.classInfo.classid,
                    navType:this.data.selectMenu.navType,
                    rankRegionNum: 50
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                //console.log(data,'ggggggggggggggggggggggggggg');
                this.setData({
                    fractionalRangeData: data[0].classParagraph.scoreRegionList
                })
                //console.log(this.data.fractionalRangeData, 'hhhhhhhhhhhhhhhhhhhh')
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 总分两分四率
    async totalScoreScoringRate() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/allscore/syn',
                method: 'POST',
                data: {
                    navId: datalist.n5,
                    groupId: datalist.groupid,
                    type: 0,
                    genExcel: 0,
                    rankRegionNum: 1
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                //console.log(data,'cxxxxxxxxxxxxxxxxxxxx');
                let datas = []
                data.synIndicatrixResponses.map(res => {
                    if (res.school == that.data.classInfo.classname) {
                        datas = [res]
                    }
                })
                let data1 = []
                let data2 = []
                data.synIndicatrixResponses.map((res,index) => {
                    if (res.school == that.data.classInfo.classname) {
                        data1 = [res.goodScoreRate,res.mediumScoreRate,res.passScoreRate,res.lowScoreRate]
                    }
                    if (index == 1) {
                        data2 = [res.goodScoreRate,res.mediumScoreRate,res.passScoreRate,res.lowScoreRate]
                    }
                })
                //console.log(data1,data2, 'gggggggggggggggggggggggg');
                this.setData({
                    yData: [
                        {
                            name: '班级',
                            data: data1
                        },
                        {
                            name: '学校',
                            data: data2
                        }
                    ],
                    scoringRateData: datas
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 单科两分四率
    async monofamilyScoringRate() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/syn/indicatrix',
                method: 'POST',
                data: {
                    navId: this.data.selectMenu.navId,
                    groupId: datalist.groupid,
                    type: 0,
                    genExcel: 0,
                },
                header: {
                    "content-type": "application/json"
                }
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                //console.log(data,'cxxxxxxxxxxxxxxxxxxxx');
                let data1 = []
                let data2 = []
                let datas = []
                if (this.data.selectMenu.navType == 0) {
                    data.synIndicatrixResponses.map((res,index) => {
                        if (res.school == that.data.classInfo.classname) {
                            data1 = [res.goodScoreRate,res.mediumScoreRate,res.passScoreRate,res.lowScoreRate]
                            datas = [res]
                        }
                        if (index == 1) {
                            data2 = [res.goodScoreRate,res.mediumScoreRate,res.passScoreRate,res.lowScoreRate]
                        }
                    })
                } else {
                    data.ffsynIndicatrixResponses.map((res,index) => {
                        if (res.school == that.data.classInfo.classname) {
                            data1 = [res.goodScoreRate,res.mediumScoreRate,res.passScoreRate,res.lowScoreRate]
                            datas = [res]
                        }
                        if (index == 1) {
                            data2 = [res.goodScoreRate,res.mediumScoreRate,res.passScoreRate,res.lowScoreRate]
                        }
                    })
                }
                this.setData({
                    yData: [
                        {
                            name: '班级',
                            data: data1
                        },
                        {
                            name: '学校',
                            data: data2
                        }
                    ],
                    scoringRateData: datas
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 上线对比
    async analysisClassOnlineComparator() {
        if (!this.data.launchFlag) {
            return
        }
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/class/online/comparator',
                method: 'POST',
                data: {
                    navId: datalist.n5,
                    groupId: that.data.classInfo.classid,
                    lineType: 0,
                },
                header: {
                    "content-type": "application/json"
                }
            }
            if (this.data.selectMenu.type4 != null) {
                options.data.type4 = [this.data.selectMenu.type4]
            }
            let res = await http(options)
            //console.info(res)
            let data = res.data.data
            if (res.data.code == 0) {
                //console.log(data,'cxxxxxxxxxxxxxxxxxxxx');
                if (data) {
                    this.setData({
                        goOnlineFlag: true
                    })
                } else {
                    this.setData({
                        goOnlineFlag: false
                    })
                }
                this.setData({
                    onlineComparison: data
                })
                this.setData({
                    lineOnes: data.one.lineScore,
                    lineTwos: data.two.lineScore,
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 获取联考是否有上线信息
    async getLaunchInformation() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            let options = {
                url: '/group/list',
                method: 'POST',
                data: {
                    navId: datalist.n5,
                },
                header: {
                    "content-type": "application/json"
                }
            }
            let res = await http(options)
            //console.info(res,'l>>>>>>>>>>>>>>>>>>>>>>>>>>l')
            let data = res.data.data
            if (res.data.code == 0) {
                if (data.lknum1 == 0 && data.lknum2 == 0 && data.wknum1 == 0 && data.wknum2 == 0) {
                    that.setData({
                        launchFlag: false
                    })
                } else {
                    // 有上线信息
                    that.setData({
                        launchFlag: true
                    })
                }
                that.getSchoolSubject()
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            classInfo: JSON.parse(wx.getStorageSync('classInfo'))
        })
        this.getLaunchInformation()
        // this.getSchoolSubject()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let query = wx.createSelectorQuery()
        query.select('#myElementss').boundingClientRect((rect) => {
            //console.log(rect, 'aaaaaaaaaaaaaaaaaaaaaaa');
            this.setData({
                height: rect.top + 10 +'px'
            })
                    
        }).exec()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})